package com.itheima.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberMapper;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.ReportMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.ReportDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    ReportMapper reportMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    MemberMapper memberMapper;
    @Override
    public List<Map<String, String>> getSetmealReport() {
       return reportMapper.getSetmealReport();
    }

    @Override
    public ReportDataVo getBusinessReportData() {

        String format = "yyyy-MM-dd";
        Date now = new DateTime();
        String nowStr = DateUtil.format(now, format);
        String beginOfWeek = DateUtil.beginOfWeek(now).toString(format);
        String endOfWeek = DateUtil.endOfWeek(now).toString(format);
        String beginOfMonth = DateUtil.beginOfMonth(now).toString(format);
        String endOfMonth = DateUtil.endOfMonth(now).toString(format);

        ReportDataVo reportDataVo = new ReportDataVo();
        reportDataVo.setReportDate(nowStr);
        reportDataVo.setTodayNewMember(memberMapper.findMemberCountByDate(nowStr));
        reportDataVo.setTotalMember(memberMapper.findMemberTotalCount());
        reportDataVo.setThisWeekNewMember(memberMapper.findMemberCountBetweenDate(beginOfWeek,endOfWeek));
        reportDataVo.setThisMonthNewMember(memberMapper.findMemberCountBetweenDate(beginOfMonth,endOfMonth));
        reportDataVo.setTodayOrderNumber(orderMapper.findOrderCountByDate(nowStr));
        reportDataVo.setTodayVisitsNumber(orderMapper.findVisitsCountByDate(nowStr));
        reportDataVo.setThisWeekOrderNumber(orderMapper.findOrderCountBetweenDate(beginOfWeek,endOfWeek));
        reportDataVo.setThisWeekVisitsNumber(orderMapper.findVisitsCountBetweenDate(beginOfWeek,endOfWeek));
        reportDataVo.setThisMonthOrderNumber(orderMapper.findOrderCountBetweenDate(beginOfWeek,endOfWeek));
        reportDataVo.setThisMonthVisitsNumber(orderMapper.findVisitsCountBetweenDate(beginOfWeek,endOfWeek));
        reportDataVo.setHotSetmeal(orderMapper.findHotSetmeal());

        return reportDataVo;
    }

    @Override
    public List<Map<String, String>> getAgeNummber() {
       // 0-18、18-30、30-45、45以上
        List<Integer> ageNummbers = memberMapper.getAgeNummber();
        System.out.println("ageNummbers = " + ageNummbers);
        List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
        Map map1=new HashMap();
        Map map2=new HashMap();
        Map map3=new HashMap();
        Map map4=new HashMap();
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;



        for (Integer ageNummber : ageNummbers) {
            if (ageNummber != null) {
                if (ageNummber > 0 && ageNummber <= 18) {
                    count1++;
                } else if (ageNummber > 18 && ageNummber <= 35) {
                    count2++;
                } else if (ageNummber > 35 && ageNummber <= 55) {
                    count3++;
                }
            }else {
                count4++;
            }
        }
        map1.put("name","45岁以上");
        map1.put("value",String.valueOf(count4));
        maps.add(map1);

        map2.put("name","0-18岁");
        map2.put("value",String.valueOf(count1));
        maps.add(map2);

        map3.put("name","19-35岁");
        map3.put("value",String.valueOf(count2));
        maps.add(map3);

        map4.put("name","36-45岁");
        map4.put("value",String.valueOf(count3));
        maps.add(map4);


        return maps;
    }
}
